import { Card, Button, Checkbox, Form, Input, message } from 'antd'
import logo from '@/assets/images/logo.png'
import { useDispatch } from 'react-redux/es/exports'

import { login } from '@/store/actions'
import { useHistory, useLocation } from 'react-router-dom'

import styles from './index.module.scss'

const Login = () => {
  const dispatch = useDispatch()
  const history = useHistory()
  const location = useLocation()
  const onFinish = async (values) => {
    // console.log(values)

    // if (!values.remeber) {
    //   message.error('请先同意协议')
    //   return
    // }

    try {
      await dispatch(login({ mobile: values.mobile, code: values.code }))
      message.success('登陆成功', 1.5, () => {
        history.replace(location.state?.from ?? '/home')
      })
    } catch (e) {
      if (e.response?.data?.message) {
        message.error(e.response?.data?.message)
      }
    }
  }
  // const onFinishFailed = ({ values, errorFields, outOfDate }) => {
  //   console.log({ values, errorFields, outOfDate })

  //     if (!/^1[3-9]\d{9}$/.test(+values.mobile)) {
  //       message.error('手机号格式不正确')
  //     } else if (!/\d{6}/.test(+values.code)) {
  //       message.error('验证码格式不正确')
  //     } else if (!values.remeber) {
  //       message.error('请先同意协议')
  //     }
  // }
  // const onValuesChange = (changedValues, allValues) => {
  //   console.log(changedValues, allValues)
  // }
  return (
    <div className={styles.root}>
      <Card
        className="login-wrap"
        bordered={false}
        bodyStyle={{
          padding: 20,
        }}
      >
        <div>
          <img className="logo" src={logo} alt="" />
        </div>
        <Form
          size="large"
          validateTrigger={['onBlur', 'onChange']}
          initialValues={{
            mobile: '13911111111',
            code: '246810',
            // remeber: false,
          }}
          onFinish={onFinish}
          // onFinishFailed={onFinishFailed}
          // onValuesChange={onValuesChange}
        >
          <Form.Item
            name="mobile"
            rules={[
              {
                required: true,
                message: '手机号为必填',
              },
              {
                pattern: /^1[3-9]\d{9}$/,
                message: '手机号格式不正确',
                validateTrigger: 'onBlur',
              },
            ]}
          >
            <Input maxLength={11} placeholder="请输入手机号码" />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              {
                required: true,
                message: '验证码为必填项',
              },
              {
                len: 6,
                message: '验证码格式不正确',
                validateTrigger: 'onBlur',
              },
            ]}
          >
            <Input maxLength={6} placeholder="请输入验证码" />
          </Form.Item>

          <Form.Item
            name="remeber"
            valuePropName="checked"
            rules={[
              {
                required: true,
                message: '请先同意协议',
              },
              // ({ getFieldValue }) => ({
              //   validator(object, value) {
              //     console.log(object, value, getFieldValue('remeber'))
              //     if (!getFieldValue('remeber')) {
              //       return Promise.reject(new Error('请先同意协议'))
              //     }
              //     return Promise.resolve()
              //   },
              // }),
            ]}
          >
            <Checkbox>我已阅读并同意「用户协议」和「隐私条款」</Checkbox>
          </Form.Item>

          <Form.Item
            style={{
              marginBottom: 0,
            }}
          >
            <Button type="primary" htmlType="submit" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
export default Login
